package cn.yutong.system.controller;


import cn.yutong.framework.RestResponse;
import cn.yutong.system.entity.InviteCode;
import cn.yutong.system.service.IInviteCodeService;
import io.swagger.annotations.Api;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 邀请码 前端控制器
 * </p>
 *
 * @author lnexin@aliyun.com
 * @since 2019-05-12
 */
@RestController
@RequestMapping("/code")
@Api(tags = "邀请码")
public class InviteCodeController {

    @Autowired
    IInviteCodeService codeService;

    @GetMapping("/create")
    public RestResponse createCode() {
        StringBuilder text = new StringBuilder();
        List<InviteCode> list = new ArrayList<>();
        for (int i = 0; i < 2000; i++) {
            InviteCode code = new InviteCode();
            String sn = DigestUtils.md5Hex(UUID.randomUUID().toString());
            sn = sn.substring(0, 6) + System.currentTimeMillis() / 1000;
            code.setCode(sn);
            code.setStatus(0);
            list.add(code);

            text.append(sn).append(",");
        }


        boolean b = codeService.insertBatch(list);
        return RestResponse.newEntity(200, "批量生成邀请码", text.toString());

    }

    public static void main(String[] args) {
        String s = UUID.randomUUID().toString();
        System.out.println(s);
    }
}

